class Solution {
public:
    int minOperations(vector<int>& nums, int x)
    {
        int left = 0;
        int right = 0;
        int n = nums.size();
        int len = 0;
        int sum = 0;
        int des = 0;
        int flag = 0;

        for (auto e : nums)
        {
            sum += e;
        }

        if (sum < x)
        {
            return -1;
        }

        while (right < n)
        {
            des += nums[right];

            while (left <n && des > sum - x)
            {
                des -= nums[left++];
            }

            if (des == sum - x)
            {
                len = max(len, right - left + 1);
                flag = 1;
            }

            right++;
        }

        return flag == 0 ? -1 : n - len;
    }
};